import 'package:basic_common/app_theme.dart';
import 'package:basic_common/common_exports.dart';
import 'package:basic_common/global_getx.dart';

class HomeController extends GetxController
    with GetSingleTickerProviderStateMixin {
  /// 统计数据
  final titles = [
    {"title": "已签约人数", "content": "0"},
    {"title": "服务总量", "content": "0"},
    {"title": "待处理服务数", "content": "0"}
  ].obs;

  /// 常用功能数据
  final commonUseList = [
    {
      "icon": "assets/home/icon_pb.png",
      "title": "我的排班",
      "page": AppRoutes.mySchedule
    },
    {
      "icon": "assets/home/icon_sign.png",
      "title": "签约管理",
      "page": AppRoutes.signmanager
    },
    {
      "icon": "assets/home/icon_jiating.png",
      "title": "家庭医护",
      "page": AppRoutes.family
    },
    {
      "icon": "assets/home/icon_fuzhen.png",
      "title": "复诊规划",
      "page": AppRoutes.followUp
    },
    {
      "icon": "assets/home/icon_suifang.png",
      "title": "随访服务",
      "page": AppRoutes.suifang
    },
    {
      "icon": "assets/home/icon_huifang.png",
      "title": "回访服务",
      "page": AppRoutes.huifang
    }
  ];

  /// 待办事项标题
  final todoTitleList = ["今日待办事项", "本周待办事项", "本月待办事项"];

  /// 待办事项列表数据
  final todoListContent = [
    {
      "icon": "assets/home/icon_qyqr.png",
      "title": "签约确认",
      "num": "0",
      "color": "0xFFFFE5E5",
      "page": AppRoutes.signConfirm
    },
    {
      "icon": "assets/home/icon_jhhf.png",
      "title": "计划回访",
      "num": "0",
      "color": "0xFFCEF3EA",
      "page": AppRoutes.signConfirm
    },
    {
      "icon": "assets/home/icon_zxhf.png",
      "title": "咨询回复",
      "num": "0",
      "color": "0xFFDBE7FF",
      "page": AppRoutes.signConfirm
    },
    {
      "icon": "assets/home/icon_yyhl.png",
      "title": "预约护理",
      "num": "0",
      "color": "0xFFF9EEE1",
      "page": AppRoutes.signConfirm
    },
    {
      "icon": "assets/home/icon_yshz.png",
      "title": "医生会诊",
      "num": "0",
      "color": "0xFFFDE8EA",
      "page": AppRoutes.signConfirm
    }
  ];
  // 用户在线状态
  final stateStatus = 0.obs;
  final onlineState = ''.obs;
  // 用户在线状态图标
  final stateIcon = 'assets/home/icon_online.svg'.obs;
  //用户在线状态label颜色
  final stateColor = AppTheme.primyColor.obs;

  /// 事件切换控制器
  TabController? tabController;

  final todoListStatis = [].obs;
  @override
  void onInit() async {
    tabController = TabController(length: todoTitleList.length, vsync: this)
      ..addListener(() {
        if (!tabController!.indexIsChanging) {
          int pageIndex = tabController!.index;
          if (pageIndex == 0) {
            getTodoStatis(
                docNo: globalGetx.doctorInfo!.doctorNo!, type: 'one-day');
          }
          if (pageIndex == 1) {
            getTodoStatis(
                docNo: globalGetx.doctorInfo != null &&
                        globalGetx.doctorInfo!.doctorNo != null
                    ? globalGetx.doctorInfo!.doctorNo!
                    : '',
                type: 'one-week');
          }
          if (pageIndex == 2) {
            getTodoStatis(
                docNo: globalGetx.doctorInfo!.doctorNo!, type: 'one-month');
          }
        }
      });
    print("对象==home--globalGetx===${globalGetx}");

    await globalGetx.getDrProfile();
    Future.delayed(const Duration(milliseconds: 800), () {
      updateDrInfo();
    });
    // 上传位置信息
    stateStatus.value = globalGetx.doctorInfo != null
        ? globalGetx.doctorInfo!.workingStatus!
        : 0;
    getDrStatus(onState: stateStatus.value);
    print("位置信息====${globalGetx.currentPosition.latitude}");
    super.onInit();
  }

  getDrStatus({required int onState}) {
    switch (onState) {
      case 0:
        onlineState.value = '在线';
        break;
      case 1:
        onlineState.value = '工作中';

        break;
      case 2:
        onlineState.value = '忙碌';

        break;
      case 4:
        onlineState.value = '停诊';
        break;
    }
  }

  void getStatisData() async {
    HomeStatisData res = await ApiService().getHomeStatis();
    titles.where((item) => item['title'] == '已签约人数').first['content'] =
        res.sign.toString() ?? '0';
    titles.where((item) => item['title'] == '服务总量').first['content'] =
        res.visit.toString() ?? '0';
    titles.where((item) => item['title'] == '待处理服务数').first['content'] =
        res.consultation.toString() ?? '0';
    update(['statis']);
  }

  /// 获取今日/本周/本月待办事项统计
  void getTodoStatis({required String docNo, required String type}) async {
    HomeStatisData res = await ApiService().getHomeStatis(data: {
      "scene": "indexPage-todoList",
      "sceneDesc": "在医生APP端首页显示医生代办事项列表，需要按类别分组排序",
      "searchKeys": {
        "doctorNo": docNo,
        "showMode": "group-report",
        "dateRange": {"type": type, "dateValue": "000"}
      }
    });

    todoListContent.where((item) => item['title'] == '签约确认').first['num'] =
        res.sign.toString() ?? '0';
    todoListContent.where((item) => item['title'] == '计划回访').first['num'] =
        res.visit.toString() ?? '0';
    todoListContent.where((item) => item['title'] == '咨询回复').first['num'] =
        res.answer.toString() ?? '0';
    todoListContent.where((item) => item['title'] == '预约护理').first['num'] =
        res.nurse.toString() ?? '0';
    todoListContent.where((item) => item['title'] == '医生会诊').first['num'] =
        res.consultation.toString() ?? '0';
    update(['todoList']);
  }

  /// 更新医生在线状态
  void updateState() async {
    int status = stateStatus.value;
    HttpData res = await ApiService().updateDrState(data: {"status": status});
    print("更新医生状态返回值===${res.toString()}");
  }

  /// 上传资料
  void updateDrInfo() async {
    double lat = globalGetx.currentPosition.latitude;
    double lng = globalGetx.currentPosition.longitude;
    HttpData res = await ApiService().updateDrInfo(data: {
      "doctorNo":
          globalGetx.doctorNo.value.isEmpty ? "" : globalGetx.doctorNo.value,
      "profilePhoto": "",
      "personalProfile": "",
      "beGoodAt": "",
      "duty": "",
      "doctorNick": "",
      "latitude": "${lat}",
      "longitude": "${lng}"
    });
    // if (res.code == 1) {
    //   ToastUtils.toast("位置信息上传成功");
    // }
  }

  /// 进入扫描二维码
  void navToScan() {
    Get.toNamed(AppRoutes.scancode, arguments: {
      "name": globalGetx.identityInfo!.realName,
      "title": globalGetx.doctorInfo!.titleName!,
      "head": globalGetx.doctorInfo!.profilePhoto ?? ''
    });
  }

  /// 上传医生信息

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
    tabController!.dispose();
  }
}
